package ui.swing.mycomps;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Editor {
	String data, save;
	ArrayList l = new ArrayList();
	MyAction ma = new MyAction();
	JFrame f = new JFrame("新建 文本文档.txt - 记事本");
	JTextArea ta = new JTextArea(30, 60);
	JScrollPane jsp = new JScrollPane(ta);
	JFileChooser jfc = new JFileChooser();
	JMenuBar mb = new JMenuBar();
	JMenu mf = new JMenu("文件(F)");
	JMenuItem mfn = new JMenuItem("新建");
	JMenuItem mfo = new JMenuItem("打开");
	JMenuItem mfs = new JMenuItem("保存");
	JMenuItem mfa = new JMenuItem("另存为");
	JMenuItem mfu = new JMenuItem("页面设置");
	JMenuItem mfp = new JMenuItem("打印");
	JMenuItem mfx = new JMenuItem("退出");
	JMenu me = new JMenu("编辑(E)");
	JMenuItem meu = new JMenuItem("撤消");
	JMenuItem met = new JMenuItem("剪切");
	JMenuItem mec = new JMenuItem("复制");
	JMenuItem mep = new JMenuItem("黏贴");
	JMenuItem mel = new JMenuItem("删除");
	JMenuItem mef = new JMenuItem("查找和替换");
	JMenuItem mea = new JMenuItem("全选");
	JMenuItem med = new JMenuItem("日期");
	JMenu mo = new JMenu("格式(O)");
	JMenuItem mow = new JMenuItem("自动换行");
	JMenuItem mof = new JMenuItem("字体");
	JMenu mh = new JMenu("帮助(H)");
	JMenuItem mhh = new JMenuItem("帮助主题");
	JMenuItem mha = new JMenuItem("关于记事本");

	JDialog ctd = new JDialog(f, "查找和替换");
	JTextField cttc = new JTextField(15);
	JTextField cttt = new JTextField(15);
	JLabel ctlc = new JLabel("查找内容");
	JLabel ctlt = new JLabel("替换为");
	JLabel ctle = new JLabel();
	JButton ctb = new JButton("查找下一个");
	JButton ctb1 = new JButton("替换");
	JButton ctb2 = new JButton("全部替换");
	JButton ctb3 = new JButton("取消");

	JFrame font = new JFrame("字体");
	JPanel fpn = new JPanel(new GridLayout(1, 3, 5, 5));
	JPanel fpc = new JPanel(new BorderLayout());
	JPanel fpcc = new JPanel(new GridLayout(1, 3, 5, 5));
	JPanel fpcn = new JPanel(new GridLayout(1, 3, 5, 5));
	JPanel fpe = new JPanel(new GridLayout(3, 1, 5, 5));
	JLabel lpl = new JLabel("字体");
	JLabel lpc = new JLabel("字形");
	JLabel lpr = new JLabel("大小");
	JLabel lyl = new JLabel("无锡欢迎你");
	JTextField tpl = new JTextField();
	JTextField tpc = new JTextField();
	JTextField tpr = new JTextField();
	JList lil = new JList();
	JScrollPane jsp1 = new JScrollPane(lil);
	String[] zx = { "常规", "粗体", "斜体", "粗斜体" };
	JList lic = new JList(zx);
	String[] dx = { "15", "20", "25", "30", "35", "40", "45", "50" };
	JList lir = new JList(dx);
	JButton bs = new JButton("确定");
	JButton be = new JButton("取消");
	JButton by = new JButton("应用");
	GraphicsEnvironment ge =

	GraphicsEnvironment.getLocalGraphicsEnvironment();
	String[] flil = ge.getAvailableFontFamilyNames();

	public Editor() {
		jfc.setCurrentDirectory(new File("d:/"));
		f.setLocation(150, 150);
		// 设置菜单栏MenuBar
		f.add(jsp, BorderLayout.CENTER);
		f.setJMenuBar(mb);
		mb.add(mf);
		mb.add(me);
		mb.add(mo);
		mb.add(mh);
		// 设置文件菜单
		mf.add(mfn);
		mf.add(mfo);
		mf.add(mfs);
		mf.add(mfa);
		mf.addSeparator();
		mf.add(mfu);
		mf.add(mfp);
		mf.addSeparator();
		mf.add(mfx);
		// 设置编辑菜单
		me.add(meu);
		me.addSeparator();
		me.add(met);
		me.add(mec);
		me.add(mep);
		me.add(mel);
		me.addSeparator();
		me.add(mef);
		me.addSeparator();
		me.add(mea);
		me.add(med);
		// 设置格式菜单
		mo.add(mow);
		mo.add(mof);
		// 设置帮助菜单
		mh.add(mhh);
		mh.addSeparator();
		mh.add(mha);
		// 设置查找和替换界面
		ctd.setLayout(new GridLayout(3, 3, 5, 5));
		ctd.add(ctlc);
		ctd.add(cttc);
		ctd.add(ctb);
		ctd.add(ctlt);
		ctd.add(cttt);
		ctd.add(ctb1);
		ctd.add(ctb2);
		ctd.add(ctle);
		ctd.add(ctb3);
		// 设置字体窗口界面
		jsp1.setHorizontalScrollBarPolicy

		(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		font.setBounds(300, 300, 400, 300);
		font.add(fpc);
		fpc.add(fpcn, BorderLayout.NORTH);
		fpc.add(fpcc, BorderLayout.CENTER);
		font.add(fpn, BorderLayout.NORTH);
		font.add(fpe, BorderLayout.EAST);
		font.add(lyl, BorderLayout.SOUTH);
		fpn.add(lpl);
		fpn.add(lpc);
		fpn.add(lpr);
		fpcn.add(tpl);
		fpcn.add(tpc);
		fpcn.add(tpr);
		fpcc.add(jsp1);
		fpcc.add(lic);
		fpcc.add(lir);
		fpe.add(bs);
		fpe.add(be);
		fpe.add(by);
		DefaultListModel fontModel = new DefaultListModel();

		for (int i = 0; i < flil.length; i++) {
			fontModel.add(i, flil[i]);
		}
		lil.setModel(fontModel);
		lil.setSelectedIndex(0);
		lic.setSelectedIndex(0);
		lir.setSelectedIndex(0);
		tpl.setText((String) lil.getSelectedValue());
		tpc.setText((String) lic.getSelectedValue());
		tpr.setText((String) lir.getSelectedValue());
		tpl.setEditable(false);
		tpc.setEditable(false);
		tpr.setEditable(false);
		// 设置快捷键
		mf.setMnemonic('f');
		me.setMnemonic('e');
		mo.setMnemonic('o');
		mh.setMnemonic('h');
		mfn.setAccelerator(KeyStroke.getKeyStroke

		(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		mfo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,

		InputEvent.CTRL_MASK));
		mfs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,

		InputEvent.CTRL_MASK));
		mfx.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,

		InputEvent.CTRL_MASK));
		meu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,

		InputEvent.CTRL_MASK));
		met.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,

		InputEvent.CTRL_MASK));
		mec.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,

		InputEvent.CTRL_MASK));
		mep.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,

		InputEvent.CTRL_MASK));
		mel.setAccelerator(KeyStroke.getKeyStroke

		(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK));
		mef.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,

		InputEvent.CTRL_MASK));
		mea.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,

		InputEvent.CTRL_MASK));
		med.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,

		InputEvent.CTRL_MASK));

		f.pack();
		f.setVisible(true);
		f.setDefaultCloseOperation

		(WindowConstants.EXIT_ON_CLOSE);
		// 添加事件
		mfn.addActionListener(ma);
		mfo.addActionListener(ma);
		mfs.addActionListener(ma);
		mfa.addActionListener(ma);
		mfx.addActionListener(ma);
		meu.addActionListener(ma);
		met.addActionListener(ma);
		mec.addActionListener(ma);
		mep.addActionListener(ma);
		mel.addActionListener(ma);
		mea.addActionListener(ma);
		med.addActionListener(ma);
		mef.addActionListener(ma);
		ctb.addActionListener(ma);
		ctb1.addActionListener(ma);
		ctb2.addActionListener(ma);
		ctb3.addActionListener(ma);
		mow.addActionListener(ma);
		mof.addActionListener(ma);
		lil.addListSelectionListener(ma);
		lic.addListSelectionListener(ma);
		lir.addListSelectionListener(ma);
		bs.addActionListener(ma);
		be.addActionListener(ma);
		by.addActionListener(ma);
	}

	/**
	 * 添加各种事件方法
	 */
	class MyAction implements ActionListener, ListSelectionListener {
		int returnVal, i, ft;
		File file;

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == mfn) {

				// 新建文本
				f.setTitle("无标题 - 记事本");
				ta.setText("");
			} else if (e.getSource() == mfo) {

				// 打开文件
				returnVal = jfc.showOpenDialog(f);
				file = jfc.getSelectedFile();
				if (returnVal ==

				JFileChooser.APPROVE_OPTION) {
					ta.setText(openFile(file));
					f.setTitle(jfc.getName(file) + " - 记事本");
				}
			} else if (e.getSource() == mfs) {

				// 保存文件
				if (f.getTitle() == "无标题 - 记事本") {
					data = ta.getText();
					returnVal = jfc.showSaveDialog

					(f);
					file = jfc.getSelectedFile();
					if (returnVal ==

					JFileChooser.APPROVE_OPTION) {
						saveFile(file, data);
						f.setTitle(jfc.getName

						(file) + " - 记事本");
					}
				} else {
					data = ta.getText();
					if (file != null) {
						saveFile(file, data);
					}
				}
			} else if (e.getSource() == mfa) {

				// 另存为
				data = ta.getText();
				returnVal = jfc.showSaveDialog(f);
				file = jfc.getSelectedFile();
				if (returnVal == JFileChooser.APPROVE_OPTION)

				{
					saveFile(file, data);
					f.setTitle(jfc.getName(file) + " - 记事本");
				}
			} else if (e.getSource() == mfx) {

				// 退出
				System.exit(0);
			} else if (e.getSource() == meu) {

				// 撤销
				if (l.size() > 0) {
					save = (String) l.get(l.size() -

					1);
					l.remove(l.size() - 1);
					ta.setText(save);
				}
			} else if (e.getSource() == met) {

				// 剪切
				l.add(ta.getText());
				ta.cut();
			} else if (e.getSource() == mec) {

				// 复制
				l.add(ta.getText());
				ta.copy();
			} else if (e.getSource() == mep) {

				// 黏贴
				l.add(ta.getText());
				ta.paste();
			} else if (e.getSource() == mel) {

				// 删除
				l.add(ta.getText());
				ta.replaceSelection(null);
			} else if (e.getSource() == mea) { // 全选
				ta.selectAll();
			} else if (e.getSource() == med) { // 时间日期
				l.add(ta.getText());
				Calendar cd = Calendar.getInstance();
				ta.setText("" + cd.get

				(Calendar.HOUR_OF_DAY) + ":" + cd.get(Calendar.MINUTE) + "  "
						+ cd.get

						(Calendar.YEAR) + "-" + (cd.get(Calendar.MONTH) + 1)
						+ "-" + cd.get

						(Calendar.DATE));
			} else if (e.getSource() == mef) { // 设置查找和替换功能
				ctd.setLocation(450, 450);
				ctd.setResizable(false);
				ctd.pack();
				ctd.setVisible(true);
			} else if (e.getSource() == ctb3) { // 退出查

				// 找和替换界面
				ctd.setVisible(false);
			} else if (e.getSource() == ctb) { // 查找下

				// 一个
				find(cttc.getText());
			} else if (e.getSource() == ctb1) { // 替换
				l.add(ta.getText());
				if (ta.getSelectedText().equals

				(cttc.getText())) {
					ta.replaceSelection

					(cttt.getText());
					find(cttc.getText());
				}
			} else if (e.getSource() == ctb2) { // 替换全

				// 部
				l.add(ta.getText());
				String all = ta.getText().replaceAll

				(cttc.getText(), cttt.getText());
				ta.setText(all);
			} else if (e.getSource() == mow) { // 自动换

				// 行
				ta.setLineWrap(true);
			} else if (e.getSource() == mof) { // 弹出字

				// 体设置窗口
				font.setVisible(true);
			} else if (e.getSource() == bs) { // 设置字

				// 体窗口确定按钮功能
				String zt = (String)

				lic.getSelectedValue();
				if (zt.equals("常规")) {
					ft = Font.PLAIN;
				} else if (zt.equals("斜体")) {
					ft = Font.ITALIC;
				} else if (zt.equals("粗体")) {
					ft = Font.BOLD;
				} else if (zt.equals("粗斜体")) {
					ft = Font.BOLD + Font.ITALIC;
				}
				ta.setFont(new Font((String)

				lil.getSelectedValue(), ft, Integer.parseInt((String)

				lir.getSelectedValue())));
				font.setVisible(false);
			} else if (e.getSource() == be) { // 设置字

				// 体窗口取消按钮功能
				font.setVisible(false);
			} else if (e.getSource() == by) { // 设置提

				// 提窗口应用按钮功能
				String zt = (String)

				lic.getSelectedValue();
				if (zt.equals("常规")) {
					ft = Font.PLAIN;
				} else if (zt.equals("斜体")) {
					ft = Font.ITALIC;
				} else if (zt.equals("粗体")) {
					ft = Font.BOLD;
				} else if (zt.equals("粗斜体")) {
					ft = Font.BOLD + Font.ITALIC;
				}
				lyl.setFont(new Font((String)

				lil.getSelectedValue(), ft, Integer.parseInt((String)

				lir.getSelectedValue())));
			}

		}

		public void valueChanged(ListSelectionEvent e) {
			if (e.getSource() == lil) {

				// 设置字体窗口选择功能
				tpl.setText((String)

				lil.getSelectedValue());
			} else if (e.getSource() == lic) { // 设置字

				// 形窗口选择功能
				tpc.setText((String)

				lic.getSelectedValue());
			} else if (e.getSource() == lir) { // 设置字

				// 体大小选择功能
				tpr.setText((String)

				lir.getSelectedValue());
			}
		}

		public void saveFile(File f, String data) {
			PrintWriter pw = null;
			try {
				pw = new PrintWriter(f);
				pw.println(data);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				pw.close();
			}
		}

		public String openFile(File f) {
			String s = "";
			StringBuffer s1 = new StringBuffer();
			BufferedReader br = null;
			try {
				br = new BufferedReader(new FileReader

				(f));
				s = br.readLine();
				while (s != null) {
					s1.append(s + "\r\n");
					s = br.readLine();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (br != null) {
						br.close();
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			return s1.toString();
		}

		// 查找文本
		public void find(String s) {
			int n = ta.getText().indexOf(s, i);
			;
			i = n + 1;
			if (n >= 0) {
				ta.select(n, n + s.length());
			} else {
				ctle.setText("找不到" + s);
			}
		}
	}
	
	public static void main(String[] args) {
		new Editor();
	}
}
